package com.rockcent.api.impl;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.rockcent.api.ApiResponse;
import com.rockcent.api.XYApi;
import com.rockcent.api.net.HttpEngine;
import com.rockcent.api.utils.EncryptUtil;
import com.rockcent.model.AddressModel;
import com.rockcent.model.CommentModel;
import com.rockcent.model.CouponBO;
import com.rockcent.model.CustCouponOrderBO;
import com.rockcent.model.CustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.rockcent.model.CustomerIncomeBO;
import com.rockcent.model.OrderInfo;
import com.rockcent.model.SchoolModel;
import com.rockcent.model.Topics;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述信息
 *
 * @author keegan
 * @date 15/8/6
 */
public class XYApiImpl implements XYApi {

    private final static String REQUEST_TIMEOUT = "连接超时，请检查网络或稍后重试";

    private HttpEngine httpEngine;

    public XYApiImpl(String serverUrl) {
        httpEngine = new HttpEngine(serverUrl);
    }

    @Override
    public ApiResponse<OrderInfo> createCustomerOrderByAliPay(String phone, int couponId, int count) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("phone", phone);
            paramMap.put("couponId", couponId);
            paramMap.put("count", count);
            Type type = new TypeToken<ApiResponse<OrderInfo>>() {}.getType();
            return httpEngine.postHandle(paramMap, type, CREATE_CUSTOMER_ORDER_BY_ALIPAY);
        } catch (Exception e) {
            return new ApiResponse<OrderInfo>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<OrderInfo> createCustomerOrderByWxPay(String phone, int couponId, int count, String appId, String mchId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("phone", phone);
            paramMap.put("couponId", couponId);
            paramMap.put("count", count);
            paramMap.put("appId", appId);
            paramMap.put("mchId", mchId);
            Type type = new TypeToken<ApiResponse<OrderInfo>>() {}.getType();
            return httpEngine.postHandle(paramMap, type, CREATE_CUSTOMER_ORDER_BY_WX_PAY);
        } catch (Exception e) {
            return new ApiResponse<OrderInfo>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Boolean> changeOrderPrice(String xyCustomerId, int orderId, double realPrice) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("orderId", orderId);
            paramMap.put("realPrice", realPrice);
            Type type = new TypeToken<ApiResponse<Boolean>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, CHANGE_ORDER_PRICE);
        } catch (Exception e) {
            return new ApiResponse<Boolean>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CustCouponOrderBO>> myCanceledOrder(String xyCustomerId, int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("currentPage", currentPage);
            paramMap.put("pageSize", pageSize);
            Type type = new TypeToken<ApiResponse<List<CustCouponOrderBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, MY_CANCELED_ORDER);
        } catch (Exception e) {
            return new ApiResponse<List<CustCouponOrderBO>>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Boolean> confirmOrder(int orderId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("orderId", orderId);
            Type type = new TypeToken<ApiResponse<Boolean>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, CONFIRM_ORDER);
        } catch (Exception e) {
            return new ApiResponse<Boolean>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Boolean> cancelOrder(int orderId, String xyCustomerId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("orderId", orderId);
            paramMap.put("xyCustomerId", xyCustomerId);
            Type type = new TypeToken<ApiResponse<Boolean>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, CANCEL_ORDER);
        } catch (Exception e) {
            return new ApiResponse<Boolean>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CouponBO>> searchCoupon4Page(String xyCustomerId, String category, String searchKey, int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("category", category);
            paramMap.put("searchKey", searchKey);
            paramMap.put("currentPage", String.valueOf(currentPage));
            paramMap.put("pageSize", String.valueOf(pageSize));
            Type type = new TypeToken<ApiResponse<List<CouponBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, SEARCH_COUPON_4_Page);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    public ApiResponse<List<CustCouponOrderBO>> myFinishedOrder(String xyCustomerId, int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("currentPage", currentPage);
            paramMap.put("pageSize", pageSize);
            Type type = new TypeToken<ApiResponse<List<CustCouponOrderBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, MY_FINISHED_ORDER);
        } catch (Exception e) {
            return new ApiResponse<List<CustCouponOrderBO>>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CustCouponOrderBO>> myUnfinishedOrder(String xyCustomerId,
                                                                  int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("currentPage", currentPage);
            paramMap.put("pageSize", pageSize);
            Type type = new TypeToken<ApiResponse<List<CustCouponOrderBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, MY_UNFINISHED_ORDER);
        } catch (Exception e) {
            return new ApiResponse<List<CustCouponOrderBO>>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Double> calcCustCouponPrice(int custCouponId, int count) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("custCouponId", custCouponId);
            paramMap.put("count", count);
            Type type = new TypeToken<ApiResponse<Double>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, CALC_CUST_COUPON_PRICE);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    public ApiResponse<Boolean> activateCoupon(int couponModelId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("couponModelId", couponModelId);
            Type type = new TypeToken<ApiResponse<Boolean>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, ACTIVATE_COUPON);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Boolean> inactivateCoupon(int couponModelId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("couponModelId", couponModelId);
            Type type = new TypeToken<ApiResponse<Boolean>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, INACTIVATE_COUPON);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CustCouponOrderBO>> listMyReceivedOrder(String xyCustomerId, int orderState, int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("orderState", String.valueOf(orderState));
            paramMap.put("currentPage", currentPage);
            paramMap.put("pageSize", pageSize);
            Type type = new TypeToken<ApiResponse<List<CustCouponOrderBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, LIST_MY_RECEIVED_ORDER);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CustomerCouponBO>> listMyCustomerCoupon(String xyCustomerId, String state, int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("xyCustomerId", xyCustomerId);
            paramMap.put("state", state);
            paramMap.put("currentPage", currentPage);
            paramMap.put("pageSize", pageSize);
            Type type = new TypeToken<ApiResponse<List<CustomerCouponBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, LIST_MY_CUSTOMER_COUPON);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CustomerCouponBO>> listCustomerCouponsForPage(String xyCustomerId, String searchKey, int currentPage, int pageSize) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            if (!TextUtils.isEmpty(xyCustomerId)) {
                paramMap.put("xyCustomerId", xyCustomerId);
            }
            if (!TextUtils.isEmpty(searchKey)) {
                paramMap.put("searchKey", searchKey);
            }
            paramMap.put("currentPage", String.valueOf(currentPage));
            paramMap.put("pageSize", String.valueOf(pageSize));
            Type type = new TypeToken<ApiResponse<List<CustomerCouponBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, LIST_CUSTOMER_COUPONS_FOR_PAGE);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CustomerCouponBO>> listRecommendCoupons(String xyCustomerId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            if (!TextUtils.isEmpty(xyCustomerId)) {
                paramMap.put("xyCustomerId", xyCustomerId);
            }
            Type type = new TypeToken<ApiResponse<List<CustomerCouponBO>>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, LIST_RECOMMEND_COUPONS);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<CustomerCouponBO> viewCustomerCouponById(int couponModelId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("couponModelId", String.valueOf(couponModelId));
            Type type = new TypeToken<ApiResponse<CustomerCouponBO>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, VIEW_CUSTOMER_COUPON_BY_ID);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<AddressModel> getPositionBySchool(int schoolId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("schoolId", String.valueOf(schoolId));
        Type type = new TypeToken<ApiResponse<AddressModel>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, GET_POSITION_BY_SCHOOL);
        } catch (Exception e) {
            return new ApiResponse<AddressModel>(REQUEST_TIMEOUT + e.toString());
        }
    }

    @Override
    public ApiResponse<String> createCustomerOrder(String phone, int couponId, String payPass, int count) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("phone", phone);
            paramMap.put("payPass", EncryptUtil.makeMD5(payPass));
            paramMap.put("couponId", String.valueOf(couponId));
            paramMap.put("count", String.valueOf(count));
            Type type = new TypeToken<ApiResponse<String>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, CREATE_CUSTOMER_ORDER);
        } catch (Exception e) {
            return new ApiResponse<String>(REQUEST_TIMEOUT);
        }
    }

    public ApiResponse<Void> registerByPhone(String phoneNum, String code, String password, int schoolId) {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("phoneNum", phoneNum);
        paramMap.put("code", code);
        paramMap.put("password", EncryptUtil.makeMD5(password));
        paramMap.put("schoolId", String.valueOf(schoolId));
        Type type = new TypeToken<ApiResponse<Void>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, REGISTER_BY_PHONE);
        } catch (Exception e) {
            return new ApiResponse<Void>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Boolean> updateCustomer(String obj) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("obj", obj);
            Type type = new TypeToken<ApiResponse<Boolean>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, UPDATE_CUSTOMER);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<Boolean> behavior(String topicId, String customerId, String behaviorType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("topicId", topicId);
        paramMap.put("customerId", customerId);
        paramMap.put("behaviorType", behaviorType);
        Type type = new TypeToken<ApiResponse<Boolean>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, BEHAVIOR);
        } catch (Exception e) {
            return new ApiResponse<Boolean>(REQUEST_TIMEOUT + e.toString());
        }
    }

    @Override
    public ApiResponse<Boolean> editCoupon(String obj) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("obj", obj);
        Type type = new TypeToken<ApiResponse<Boolean>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, EDIT_COUPON);
        } catch (Exception e) {
            return new ApiResponse<Boolean>(REQUEST_TIMEOUT + e.toString());
        }
    }

    @Override
    public ApiResponse<Integer> publishCoupon(String obj) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("obj", obj);
        Type type = new TypeToken<ApiResponse<Integer>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, PUBLISH_COUPON);
        } catch (Exception e) {
            return new ApiResponse<Integer>(REQUEST_TIMEOUT + e.toString());
        }
    }

    /**
     * App登录 这个是校园神器的接口
     *
     * @param phoneNum
     * @param password
     * @param loginOS
     * @param imei
     * @param appleToken
     * @return
     */
    @Override
    public ApiResponse<Void> loginByApp(String phoneNum, String password, int loginOS, String imei, String appleToken) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("loginName", phoneNum);
        paramMap.put("loginOS", loginOS + "");
        paramMap.put("password", EncryptUtil.makeMD5(password));
        paramMap.put("imei", imei);
        paramMap.put("appleToken", appleToken);
        Type type = new TypeToken<ApiResponse<Void>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, LOGIN_BY_APP);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CommentModel>> listComment(int couponModelId, int currentPage, int pageSize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("couponModelId", couponModelId);
        paramMap.put("currentPage", currentPage);
        paramMap.put("pageSize", pageSize);
        Type type = new TypeToken<ApiResponse<List<CommentModel>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, LIST_COMMENT);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<CouponBO>> getTopicItemDetail(String topicId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("topicId", topicId);
        Type type = new TypeToken<ApiResponse<List<CouponBO>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, GET_TOPIC_ITEM_DETAIL);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    @Override
    public ApiResponse<List<Topics>> getTopics(int currentPage, int pageSize, String typeOfTopics) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("currentPage", currentPage);
        paramMap.put("pageSize", pageSize);
        paramMap.put("type", typeOfTopics);
        Type type = new TypeToken<ApiResponse<List<Topics>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, GET_TOPICS);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT + " Exception: "+ e.toString());
        }
    }

    public ApiResponse<CustomerBO> getCustomer(String loginName, String xyCustomerId, String customerId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("phone", loginName);
        paramMap.put("xyCustomerId", xyCustomerId);
        paramMap.put("customerId", customerId);
        Type type = new TypeToken<ApiResponse<CustomerBO>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, GET_CUSTOMER);
        } catch (Exception e) {
            return new ApiResponse<CustomerBO>(REQUEST_TIMEOUT);
        }
    }


    public ApiResponse<List<SchoolModel>> getSchools() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Type type = new TypeToken<ApiResponse<List<SchoolModel>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap, type, GET_SCHOOLS);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }

    /**
     * 查询用户收入
     *
     * @param customerId
     * @return
     */

    @Override
    public ApiResponse<CustomerIncomeBO> queryXyIncome(String customerId) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("customerId", customerId);
            Type type = new TypeToken<ApiResponse<CustomerIncomeBO>>() {
            }.getType();
            return httpEngine.postHandle(paramMap, type, QUERY_XY_INCOME);
        } catch (Exception e) {
            return new ApiResponse<>(REQUEST_TIMEOUT);
        }
    }
}
